package org.xdq.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xdq.dao.HomeDao;
import org.xdq.dto.MenuDto;
import org.xdq.model.Module;
import org.xdq.service.HomeService;

@Service
@Transactional
public class HomeServiceImpl implements HomeService {
	
	@Autowired
	private HomeDao homeDao;

	@Override
	public List<MenuDto> getMenuList(String userId) {
		
		List<Module> list = homeDao.findUserMenuList(userId);
		
		
		List<MenuDto> mainList = new ArrayList<MenuDto>();
		
		MenuDto currMain = null;//当前主菜单
		
		for(Module module : list) {
			
			if(currMain == null || !module.getP_id().equals(currMain.getMenuId())) {
				
				//创建新的主菜单
				currMain = new MenuDto();
				currMain.setMenuId(module.getP_id());
				currMain.setMenuName(module.getP_name());
				currMain.setSubMenuList(new ArrayList<MenuDto>());
				
				mainList.add(currMain);
				
			}
			
			MenuDto sub = new MenuDto();
			sub.setMenuId(module.getM_id());
			sub.setMenuName(module.getM_name());
			sub.setMenuUrl(module.getM_url());
			
			currMain.getSubMenuList().add(sub);
			
		}		
		
		return mainList;
	}

}
